/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seGobangClient.uiSystem2D;

/**
 *
 * @author Leo
 */
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import java.io.*;
import java.net.*;
import java.util.*;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.vecmath.Point2i;
import seGobangClient.IMessageBox;
import seGobangClient.ISceneController;
import seGobangClient.Options;
import seGobangClient.Rule;
import seGobangClient.gaming.IPlayer;
import seGobangClient.onlineHall.Desk;
import seGobangClient.onlineHall.PlayerInfo;
import seGobangClient.onlineHall.Room;

public class SceneManager implements seGobangClient.IUISystem,
        seGobangClient.IMessageBox, seGobangClient.mainMenu.IMainMenuScene,
        seGobangClient.gaming.IGamingScene, seGobangClient.configMenu.IConfigScene,
        seGobangClient.onlineHall.IOnlineHallScene
{

    static seGobangClient.ISceneController MyCurrentController;
    private JFrame frame;
    private GameScene GS;
    private MainScene MS;
    private OptionScene OS;
    private OnlineScene OLS;
    private getpane mainPane = new getpane(640, 448, "");
    private Font font;
    //private Music backMusic = new Music();
    //ImageIcon back=new ImageIcon("C:\\Leo\\Software Engineering\\SEChess\\images\\back.jpg");
    public SceneManager(JFrame container)
    {
        frame = container;
        container.addWindowListener(new WindowAdapter()
        {

            public void windowClosing(WindowEvent e)
            {
                SceneManager.MyCurrentController.doEvent("ExitGame", null);
            }
        });
        font = new Font("宋体", Font.PLAIN, 13);
        MS = new MainScene();
        GS = new GameScene();
        OS = new OptionScene();
        OLS = new OnlineScene();

    }

    public int showMessageBox(int Type, String Title, String Text, String CallBackEventName)
    {
        UIManager.put("OptionPane.messageFont", font);
        UIManager.put("OptionPane.buttonFont", font);
        int response = -1;
        if (Type == 2)
        {
            Object[] options = {"是", "否"};
            if (Type == IMessageBox.TYPE_QUESTION)
            {
                response = JOptionPane.showOptionDialog(null, Text, Title, JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE,
                        null, options, options[1]);
            } else
            {
                response = JOptionPane.showOptionDialog(null, Text, Title, JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE,
                        null, options, options[1]);
            }
            if (response == 0)
            {
                if (CallBackEventName != null)
                {
                    MyCurrentController.doEvent(CallBackEventName, 1);
                }
                return 1;
            } else if (response == 1)
            {
                if (CallBackEventName != null)
                {
                    MyCurrentController.doEvent(CallBackEventName, 0);
                }
                return 0;
            } else
            {
                if (CallBackEventName != null)
                {
                    MyCurrentController.doEvent(CallBackEventName, 0);
                }
                return 0;
            }
        } else
        {
            Object[] options = {"确定"};
            if (Type == IMessageBox.TYPE_QUESTION)
            {
                JOptionPane.showOptionDialog(null, Text, Title, JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE,
                        null, options, options[0]);
            } else
            {
                JOptionPane.showOptionDialog(null, Text, Title, JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE,
                        null, options, options[0]);
            }
            // System.out.println(response);
            if (CallBackEventName != null)
            {
                MyCurrentController.doEvent(CallBackEventName, 1);
            }
            return 1;
        }

    }

    public void setCurrentController(ISceneController SController)
    {
        MyCurrentController = SController;
    }

    public int setHotItemID(int HotItemID)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void currentSceneChangeTo(String SceneName)
    {
        if (SceneName.equals("MainMenu"))
        {
            
            GS.clear();
            OS.clear();
            OLS.clear();
            MS.initMainUI(frame, mainPane);
        } else if (SceneName.equals("ConfigManu"))
        {
            GS.clear();
            OS.clear();
            OLS.clear();
            OS.initOptionUI(frame, mainPane);
        } else if (SceneName.equals("Gaming"))
        {
            GS.clear();
            OS.clear();
            OLS.clear();
            GS.initGameUI(frame, mainPane);
        } else if (SceneName.equals("OnlineHall"))
        {
            GS.clear();
            OS.clear();
            OLS.clear();
            OLS.initLoginScene(frame, mainPane);
        }
    }

    public void setChess(Point2i p, byte flag)
    {
        GS.addChess(p, flag);
    }

    public void clearChessBoard()
    {
        GS.clearChessBoard();
    }

    public void setChessBoardPickable(boolean isPickable)
    {
        GS.setValid(isPickable);
    }

    public void updatePlayerInfo(IPlayer blackPlayer, IPlayer whitePlayer)
    {
        GS.updatePlayerInfo(blackPlayer, whitePlayer);
    }

    public void initPlayerInfo(IPlayer blackPlayer, IPlayer whitePlayer) {
        GS.updatePlayerInfo(blackPlayer, whitePlayer);
    }

    public void setCurrentPlayer(byte ChessFlag)
    {
        GS.setCurrentPlayer(ChessFlag);
    }

    public void AIFx(Point2i pos, int FxID) {
        if (FxID==this.AIFx_PUT)
            MyCurrentController.doEvent("ChooseChessBoard", pos);
    }
    
    public int getGraphic_Message()
    {
        return OS.getGraphic_Message();
    }

    public int getMusicVolum_Message()
    {
        return OS.getMusicVolum_Message();
    }

    public int getSFXVolum_Message()
    {
        return OS.getSFXVolum_Message();
    }

    public int getTimeLimit_Message()
    {
        return OS.getTimeLimit_Message();
    }

    public int getRegretTimes_Message()
    {
        return OS.getRegretTimes_Message();
    }

    public void Options_perform(Options op)
    {
        OS.Options_perform(op);
    }

    public void Rule_perform(Rule rl)
    {
        OS.Rule_perform(rl);
    }

    public boolean getforbids_Message()
    {
        return OS.getforbids_Message();
    }

    public void changeToLogin()
    {
        OLS.clear();
        OLS.initLoginScene(frame, mainPane);
    }

    public void changeToRoomList()
    {
        OLS.clear();
        OLS.initRoomChooseScene(frame, mainPane);
    }

    public void changeToRoom(String roomname)
    {
        OLS.clear();
        OLS.initRoomScene(frame,mainPane,roomname);
    }

    public void changeToDesk(int id)
    {
        GS.clear();
        OLS.clear();
        OLS.initDeskScene(frame, mainPane, id);
    }

    public void refreshRoomList(Room[] Rooms)
    {
        OLS.refreshRoom(Rooms);
    }

    public void refreshDeskState(Desk[] Deskes)
    {
        OLS.refreshDeskState(Deskes);
    }

    public void refreshPlayerInfo(PlayerInfo playerInfo)
    {
        OLS.refreshPlayerInfo(playerInfo);
    }

    public void printHelpInfo(String helpInfo) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
    
    
}
